import { url } from '@/config';
// let url="/api/Cgs/Portal"
// var request = window.axios;
// let VisualType = "Visual";
import request from '@/router/axios';
export const getList = (params = {}) => request({
  url: url + '/Visual/List',
  method: 'get',
  params: params
});

export const getCategory = (params = {}) => {
  return new Promise((resolve, reject) => {
    request({
        url: url + '/Category/List',
        method: 'get',
        params: params
    }).then((result) => {
        if(result?.data.success){
            resolve(result.data.data)
        }else{
            reject(new Error("未请求到分类列表"))
        }
    }).catch((err) => {
        reject(err)
    });
  })
}

export const uploadFile = (data) => {
  return new Promise((resolve, reject) => {
    request({
        url: '/api/upload-file',
        method: 'post',
        data: data,
        headers: { "Content-Type": "multipart/form-data" },
    }).then((result) => {
        resolve(result.data)
    }).catch((err) => {
        reject(err)
    });
  })
}

export const getObj = (params) => request({
  url: url + '/Visual/Detail',
  method: 'get',
  params: params
});
export const uploadImg = (formData) => request({
  url: url + '/Visual/PutFile',
  method: 'post',
  // data: 'file=' + file,
  data: formData,
  headers: { "Content-Type": "multipart/form-data" }
});

export const addObj = (data = {}) => {
  let detail = {
    name: data.title,
    width: data.width,
    height: data.height,
    scale: 1,
    backgroundImage: '/img/bg/bg1.png',
    url: '',
    mark: {},
    gradeShow: false,
    gradeLen: 30,
  }
  if( data.VisualType === 'Workflow') {
    detail = data.detail || {
      name: data.title,
      nodeList: [],
      lineList: []
    }
  }
  return request({
    url: url + '/Visual/Add',
    method: 'post',
    data: {
      Visual: {
        Password: data.password,
        Category: parseInt(data.category),
        OrgCategory: parseInt(data.orgCategory),
        Status: parseInt(data.status),
        Title: data.title,
      },
      Config: {
        Detail: JSON.stringify(detail),
        sceneUrl: data.sceneUrl,
        sceneZipUrl: data.sceneZipUrl,
      },
      VisualType: data.VisualType,
      isPortal: data.isPortal
    }
  })
};

export const updateComponent = (data = {}) => request({
  url: url + '/Visual/Edit',
  method: 'post',
  data: data,
  VisualType: data.VisualType
});

export const updateWorkflow = (data = {}) => request({
  url: url + '/Visual/Edit',
  method: 'post',
  data: {
    "Visual": {
      "ID": data.id
    },
    "Config": {
      Detail: JSON.stringify(data.detail),
      "ID": data.configId,
      autoTrigger: data.autoTrigger,
      requestData: JSON.stringify(data.requestData)
    },
    VisualType: data.VisualType
  }
});

export const updateObj = (data = {}) => request({
  url: url + '/Visual/Edit',
  method: 'post',
  data: {
    "Visual": {
      "ID": data.id,
      "Password": data.password,
      "Category": parseInt(data.category),
      "orgCategory": parseInt(data.orgCategory),
      "Status": parseInt(data.status),
      "Title": data.title,
    },
    "Config": {
      sceneUrl: data.sceneUrl,
      sceneZipUrl: data.sceneZipUrl,
    },
    VisualType: data.VisualType
  }
});

export const delObj = (params) => request({
  url: url + '/Visual/Delete',
  method: 'post',
  params: params
});

export const uploadZip = (data) => request({
  url: url + '/Import',
  method: 'post',
  data: data,
  headers: { "Content-Type": "multipart/form-data" },
  timeout: 1000 * 60 * 10
});

export const uploadApp = (data) => request({
  url: '/api/app/import',
  method: 'post',
  data: data,
  headers: { "Content-Type": "multipart/form-data" },
  timeout: 1000 * 60 * 5
});
